// DiplomacyExtensions.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.Linq;

using Supremacy.Entities;

namespace Supremacy.Diplomacy
{
    public static class DiplomacyExtensions
    {
        private static bool IsTreatyClause(this ClauseType clause)
        {
            switch (clause)
            {
                case ClauseType.TreatyAlliance:
                case ClauseType.TreatyCeaseFire:
                case ClauseType.TreatyDeclareWar:
                case ClauseType.TreatyMilitaryPact:
                case ClauseType.TreatyPeace:
                case ClauseType.TreatyResearchPact:
                case ClauseType.TreatyTradePact:
                    return true;
            }
            return false;
        }

        public static bool ExcludesOnSameSide(this ClauseType clause, ClauseType otherClause)
        {
            if (clause == ClauseType.NoClause)
                return false;
            if (IsTreatyClause(clause))
                return true;
            return false;
        }

        public static bool ExcludesOnOtherSide(this ClauseType clause, ClauseType otherClause)
        {
            if (clause == ClauseType.NoClause)
                return false;
            if (IsTreatyClause(clause))
                return true;
            return false;
        }

        public static RegardEventCategories GetCategories(this RegardEventType eventType)
        {
            RegardEventCategories categories = RegardEventCategories.None;
            switch (eventType)
            {
                case RegardEventType.LostBattle:
                    categories |= RegardEventCategories.MilitaryPower;
                    break;
                case RegardEventType.AttackedCivilians:
                    categories |= RegardEventCategories.MilitarySafety;
                    break;
                case RegardEventType.PeacetimeBorderIncursion:
                    categories |= RegardEventCategories.MilitarySafety;
                    break;
                case RegardEventType.BorderIncursionPullout:
                    categories |= RegardEventCategories.MilitarySafety;
                    break;
                case RegardEventType.InvaderMovement:
                    categories |= RegardEventCategories.MilitarySafety;
                    break;
                case RegardEventType.UnprovokedAttack:
                    categories |= RegardEventCategories.MilitarySafety;
                    break;
                case RegardEventType.ViolatedPeaceTreaty:
                    categories |= RegardEventCategories.Diplomacy;
                    break;
                case RegardEventType.ViolatedStopRaiding:
                    categories |= RegardEventCategories.Diplomacy;
                    break;
                case RegardEventType.ViolatedStopSpying:
                    categories |= RegardEventCategories.Diplomacy;
                    break;
                case RegardEventType.EnemySharesQuadrant:
                    categories |= RegardEventCategories.Diplomacy;
                    break;
                case RegardEventType.DeclaredWar:
                    // None
                    break;
                case RegardEventType.CapturedColony:
                    categories |= RegardEventCategories.MilitarySafety;
                    break;
            }
            return categories;
        }

        public static void OnAttack(this Diplomat source, Civilization aggressor)
        {
            if (source != null)
            {
                var data = source.GetExtendedData(aggressor);
                if (data != null)
                {
                    data.OnAttack();
                }
            }
        }

        public static void OnIncursion(this Diplomat source, Civilization aggressor)
        {
            if (source != null)
            {
                var data = source.GetExtendedData(aggressor);
                if (data != null)
                {
                    data.OnIncursion();
                }
            }
        }

        public static IProposal GetLastProposalSent(this Diplomat source, Civilization civ)
        {
            if (source != null)
            {
                var envoy = source.GetAlien(civ);
                if (envoy != null)
                {
                    return envoy.LastProposalSent;
                }
            }
            return null;
        }

        public static IProposal GetLastProposalReceived(this Diplomat source, Civilization civ)
        {
            if (source != null)
            {
                var envoy = source.GetAlien(civ);
                if (envoy != null)
                {
                    return envoy.LastProposalReceived;
                }
            }
            return null;
        }

        public static IResponse GetLastResponseSent(this Diplomat source, Civilization civ)
        {
            if (source != null)
            {
                var envoy = source.GetAlien(civ);
                if (envoy != null)
                {
                    return envoy.LastResponseSent;
                }
            }
            return null;
        }

        public static IResponse GetLastResponseReceived(this Diplomat source, Civilization civ)
        {
            if (source != null)
            {
                var envoy = source.GetAlien(civ);
                if (envoy != null)
                {
                    return envoy.LastResponseReceived;
                }
            }
            return null;
        }

        public static bool IsEmbargoInPlace(this Diplomat source, Civilization civ)
        {
            if (source != null)
            {
                var envoy = source.GetAlien(civ);
                if (envoy != null)
                {
                    return envoy.IsEmbargoInPlace;
                }
            }
            return false;
        }
    }
}